/**
 * Created by wry on 2015/1/13.
 */

var dailyReportService = require('../../services/BI/BI-dailyReportingService');
var messageUtil = require('../../common/utils/messageUtil');
var toolUtil = require('../../common/utils/toolUtil');
var moment = require('moment');

Date.prototype.format = function (format) {
    var o = {
        "M+": this.getMonth() + 1, //month
        "d+": this.getDate(), //day
        "h+": this.getHours(), //hour
        "m+": this.getMinutes(), //minute
        "s+": this.getSeconds(), //second
        "q+": Math.floor((this.getMonth() + 3) / 3), //quarter
        "S": this.getMilliseconds() //millisecond
    }
    if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
        (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o) if (new RegExp("(" + k + ")").test(format))
        format = format.replace(RegExp.$1,
            RegExp.$1.length == 1 ? o[k] :
                ("00" + o[k]).substr(("" + o[k]).length));
    return format;
}
function newIncrementDate(startDate, dayIncrement){
    var tempdate = new Date(startDate)
    var newdate = new Date(tempdate.setDate(tempdate.getDate() + dayIncrement));
    return newdate
}

exports.getCommunityMainStatsReport = function (req, res) {
    var community = req.params.community;
    var date = req.params.date;

    dailyReportService.getCommunityMainStatsService(community, date, function (err, results) {
        if (err) {
            logger.error('Exception happened when getCommunityMainStatsService...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when getCommunityMainStatsService...!' + err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, results));
        }
    });
};

exports.getCommunityDauStatsReport = function (req, res) {
    var community = req.params.community;
    var date = req.params.date;

    dailyReportService.getCommunityDauStatsService(community, date, function (err, results) {
        if (err) {
            logger.error('Exception happened when getCommunityDauStatsService...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when getCommunityDauStatsService...!' + err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, results));
        }
    });
};

//exports.getRetentionByCommReport = function (req, res) {
//    var community = req.params.community;
//    var interval = req.params.interval;
//
//    dailyReportService.getRetentionByCommService(community, interval, function (err, results) {
//        if (err) {
//            logger.error('Exception happened when getRetentionByCommReport...!' + err.message);
//            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when getRetentionByCommReport...!' + err.message));
//        } else {
//            res.json(messageUtil.composeSucceededMessage(req, null, results));
//        }
//    });
//}

exports.getIntervalRetentionByCommReport = function (req, res) {
    var interval = req.params.interval;
    var community = req.params.community;
    var startDate = req.params.startDate;
    var endDate = req.params.endDate;

    dailyReportService.getIntervalRetentionByCommService(interval, community, startDate, endDate, function (err, results) {
        if (err) {
            logger.error('Exception happened when getIntervalRetentionByCommReport...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when getIntervalRetentionByCommReport...!' + err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, results));
        }
    });
}


//exports.getRetentionByCommAndBatchReport = function (req, res) {
//    var community = req.params.community;
//    var userBatchDate = req.params.userBatchDate;
//    var interval = req.params.interval;
//
//    dailyReportService.getRetentionByCommAndBatchService(community, userBatchDate, interval, function (err, results) {
//        if (err) {
//            logger.error('Exception happened when getRetentionByCommAndBatchReport...!' + err.message);
//            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when getRetentionByCommAndBatchReport...!' + err.message));
//        } else {
//            res.json(messageUtil.composeSucceededMessage(req, null, results));
//        }
//    });
//}
//
